package cn.eonshine.cuba.lsms.modular.basic.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.util.DateUtils;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.basic.entity.Oiltank;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.basic.entity.OilTankInfo;
import cn.eonshine.cuba.lsms.modular.basic.service.IOilTankInfoService;
import cn.eonshine.cuba.lsms.modular.basic.service.IOilTankService;

@Service
public class OilTankService extends CommonService<Oiltank> implements IOilTankService {

	@Autowired
	private IOilTankInfoService tankinfoService;
	
	private final String listUrl = "/oiltank/list";
	private final String updateUrl = "/oiltank/update";

	@Override
	public void requestOil() {
		requestData(listUrl, updateUrl, InfStatus.INF_LOG_TYPE.OIL_TANK_NUM);
	}

	@Override
	protected Object dataHandle(Oiltank oiltank) throws Exception {
		QueryWrapper<OilTankInfo> entity = new QueryWrapper<OilTankInfo>();
		//entity.where("OIL_DEPOT_ID={0} and  OIL_TANK_CODE={1}", oiltank.getOilDepotId(),oiltank.getOilTankCode());
		entity.lambda().eq(OilTankInfo::getOilDepotId, oiltank.getOilDepotId());
		entity.lambda().eq(OilTankInfo::getOilTankCode, oiltank.getOilTankCode());
		
		OilTankInfo info = tankinfoService.getOne(entity);
		boolean isInsert = false;
		if (info == null) {
			info = new OilTankInfo();
			isInsert = true;
		}
		info.setOilDepotId(oiltank.getOilDepotId());
		info.setOilId(oiltank.getOilId());
		info.setOilTankCode(oiltank.getOilTankCode());
		info.setOilTankName(StringUtils.trim(oiltank.getOilTankName()));
		info.setUseable(oiltank.getUseable());
		
		info.setMinTankCapacity(oiltank.getMinTankCapacity());
		info.setSafetyTankCapacity(oiltank.getSafetyTankCapacity());
		info.setTankCapacity(oiltank.getTankCapacity());
		info.setRemarks(oiltank.getRemarks());
		info.setIsFromInf(InfStatus.IS_FROM_INTERFACE);
		
		Date nowTime = DateUtils.getCubaDate(Calendar.getInstance().getTime());
		if (isInsert) {
			info.setCreateBy(InfStatus.CREATE_BY);
			info.setCreateDate(nowTime);
		}
		info.setUpdateBy(InfStatus.UPDATE_BY);
		info.setUpdateDate(nowTime);
		
		if (isInsert) {
			tankinfoService.save(info);
		} else {
			tankinfoService.updateById(info);
		}
		return info;
	}

	@Override
	protected List<Oiltank> feadBackDataHandle(Object obj, Oiltank oiltank, List<Oiltank> feadBackDataLists) throws Exception {
		OilTankInfo info  = (OilTankInfo) obj;
		Oiltank tmpBack = new Oiltank();
		tmpBack.setId(info.getId());
		tmpBack.setOilDepotId(oiltank.getDmzOilDepotId());
		tmpBack.setOilId(oiltank.getDmzOilId());
		tmpBack.setDataInputDatetime(oiltank.getDataInputDatetime());
		tmpBack.setLsmsUpdate(InfStatus.LSMS_UPDATE_EXTRACT_YES);
		tmpBack.setOilTankCode(oiltank.getOilTankCode());
		tmpBack.setOilTankName(oiltank.getOilTankName());
		feadBackDataLists.add(tmpBack);
		return feadBackDataLists;
	}
}
